package com.masterclient.app.modules.aftersale.contract;

import android.widget.Toast;

import com.eoner.common.bean.base.CommonBaseBean;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.aftersale.ShipmentsBean;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/08/27
*/
public class AddReturnLogistPresenter extends AddReturnLogistContract.Presenter{

    @Override
    public void getShipmentItems() {
        Observable<ShipmentsBean> observable = RetrofitUtil.getInstance().getShipmentItems();
        Disposable disposable = toSubscribe(observable, new Consumer<ShipmentsBean>() {
            @Override
            public void accept(ShipmentsBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showShipmnets(bean.getData().getSh_shipment_companys());
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void setAfterSalesShipment(String company, String refund_id, String track_no,String code) {
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().setAfterSaleShipment(company,refund_id,track_no,code);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showSuccess("快递信息已提交");
                }else {
                    mView.showSuccess("0");
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void orderShipment(String order_express_id, String track_no, String carrier_code) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("order_express_id",order_express_id);
            jsonObject.put("track_no",track_no);
            jsonObject.put("carrier_code",carrier_code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().dealerUpdateLogistics(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showResult(true,"快递信息已提交");
                }else {
                    mView.showResult(false,bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                o.printStackTrace();
                mView.showResult(false,"网络异常，请重试");
            }
        }));
    }

    @Override
    public void setAfterSalesPurchaseShipment(String refund_id, String company, String code, String track_no) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("refund_id",refund_id);
            jsonObject.put("company",company);
            jsonObject.put("code",code);
            jsonObject.put("track_no",track_no);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().afterSalePurchaseShipment(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showSuccess("0");
                }else {
                    mView.showSuccess(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                o.printStackTrace();
                mView.showSuccess("网络异常，请重试");
            }
        }));
    }

}